/*******************************************************************************
 * Copyright 2007 Vidar Svansson
 *
 * This file is part of JAdapter.
 *
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

package org.jadapter;

import java.util.Collection;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: vs
 * Date: 25.3.2008
 * Time: 15:51:20
 */
public abstract class Function<R,A> {

    public abstract R invoke(A arg);

    public static <T> void filter(Collection<T> c, Function<Boolean,T> function) {
        for (Iterator<T> it = c.iterator(); it.hasNext();)
            if (!function.invoke(it.next())) it.remove();
    }

    public static <T> T find(Collection<T> c, Function<Boolean,T> function) {
        for(T t : c) if (function.invoke(t)) return t;
        return null;
    }

}
